package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ Author：祝文龙
 * @ Date：2023-11-05-11:08
 * @ Version：1.0
 *
 */
@Service
@Slf4j
public class MediaProcessServiceImpl implements MediaProcessService {

    @Autowired
    MediaFilesMapper mediaFilesMapper;

    @Autowired
    MediaProcessMapper mediaProcessMapper;

    @Autowired
    MediaProcessHistoryMapper mediaProcessHistoryMapper;



    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardIndex, shardTotal, count);
        return mediaProcesses;
    }
    @Override
    //实现如下
    public boolean startTask(long id) {
        int result = mediaProcessMapper.startTask(id);
        return result<=0?false:true;
    }
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {

//        要更新的任务
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
    if (mediaProcess==null){
        return;
    }

//    如果任务执行失败
        if(status.equals("3")){
            //        更新mediaprocess表的状态
            mediaProcess.setStatus("3");
            mediaProcess.setFailCount(mediaProcess.getFailCount()+1);//失败次数加1
            mediaProcess.setErrormsg(errorMsg);
            mediaProcessMapper.updateById(mediaProcess);
//            更高效的更新方式
           /* LambdaQueryWrapper<MediaProcess> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(MediaProcess::getId,taskId);
            MediaProcess mediaProcess_u = new MediaProcess();
            mediaProcess_u.setStatus("3");
            mediaProcess_u.setErrormsg(errorMsg);
            mediaProcess_u.setFailCount(mediaProcess.getFailCount()+1);
            mediaProcessMapper.update(mediaProcess_u,queryWrapper);*/
            return;
            }
//        如果执行成功
//        文件表的记录
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
//        更新media_file表中的url
        mediaFiles.setUrl(url);
        mediaFilesMapper.updateById(mediaFiles);
//        更新mediaprocess表的状态
            mediaProcess.setStatus("2");
            mediaProcess.setCreateDate(LocalDateTime.now());
            mediaProcess.setUrl(url);
            mediaProcessMapper.updateById(mediaProcess);
//        将mediaprocess表记录插入到mediaprocesshistory表
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);
         mediaProcessHistoryMapper.insert(mediaProcessHistory);
//        从mediaprocess删除当前任务
        mediaProcessMapper.deleteById(taskId);
    }

}
